﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using NHibernateDB;
using SmartAnnotation;


namespace MessMain
{
    public partial class TabItems : TabPage
    {
        public bool isConf = false;
        public ArrayList listUsers= new ArrayList();
        private static TabItems instance;
        private string limba;
        private string FTPip;
        private int FTPport;
        private string FTPuser;
        private string FTPpass;

        int _previouslySortedColumnlvDateGenerale = -1;
        int _previouslySortedColumnlvEvaluareSisteme = -1;
        int _previouslySortedColumnlvEvaluareSociala = -1;
        int _previouslySortedColumnlvExamenOncologic = -1;
        int _previouslySortedColumnlvFisaExternare = -1;
        int _previouslySortedColumnlvFisaInternare = -1;
        int _previouslySortedColumnlvFunctiiVitale = -1;
        int _previouslySortedColumnlvGraficADS = -1;
        int _previouslySortedColumnlvIgiena = -1;
        int _previouslySortedColumnlvImbracat = -1;
        int _previouslySortedColumnlvInterventieChirurgicala = -1;
        int _previouslySortedColumnlvInvestigatii = -1;
        int _previouslySortedColumnlvMedicatii = -1;
        int _previouslySortedColumnlvPersoanaTrimitere = -1;
        int _previouslySortedColumnlvPersoanaContact = -1;
        int _previouslySortedColumnlvUtilitati = -1;

        Dictionary<int, string> senzorNume = new Dictionary<int, string>();
        string CNP;

        public TabItems(string limba)
        {
            instance = this;
            InitializeComponent();

            Rename(limba);

            this.limba = limba;
            this.FTPip = Comunication.FTPIp;
            this.FTPport = Comunication.FTPPort;
            this.FTPuser = Comunication.FTPUser;
            this.FTPpass = Comunication.FTPPass;
        }


        public void loadpacient(string CNP)
        {
            this.CNP = CNP;
            //dateGenerale
            lvDateGenerale.View = View.Details;
            lvDateGenerale.FullRowSelect = true;
            lvDateGenerale.GridLines = false;
            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            List<string> namesForm = xmlParser.ExtractAttribute(lvDateGenerale.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvDateGenerale.Columns.Add(s);

            PersistenceManager pm = PersistenceManager.Instance;
            IList<DateGeneralePacient> lista = pm.RetrieveEquals<DateGeneralePacient>("IdPacient", CNP);
            lvDateGenerale.Items.Clear();
            foreach (DateGeneralePacient dgp in lista)
            {
                ListViewItem item = new ListViewItem(dgp.IdPacient);
                item.SubItems.Add(dgp.Nume);
                item.SubItems.Add(dgp.Prenume);
                item.SubItems.Add(dgp.Sex);
                item.SubItems.Add(dgp.DataNasterii.ToString());
                item.SubItems.Add(dgp.LoculNasterii);
                item.SubItems.Add(dgp.Resedinta);
                item.SubItems.Add(dgp.DomiciliuLegal);
                item.SubItems.Add(dgp.GrupaSanguina);
                item.SubItems.Add(dgp.Ocupatie);
                item.SubItems.Add(dgp.LocMunca);
                item.SubItems.Add(dgp.TalonPensie);
                lvDateGenerale.Items.Add(item);
            }

            foreach (ColumnHeader ch in this.lvDateGenerale.Columns)
            {
                ch.Width = -2;
            }


            //evaluare sisteme
            lvEvaluareSisteme.View = View.Details;
            lvEvaluareSisteme.FullRowSelect = true;
            lvEvaluareSisteme.GridLines = false;
            namesForm = xmlParser.ExtractAttribute(lvEvaluareSisteme.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvEvaluareSisteme.Columns.Add(s);

            IList<EvaluareSisteme> lista2 = pm.RetrieveEquals<EvaluareSisteme>("IdPacient", CNP);
            lvEvaluareSisteme.Items.Clear();
            foreach (EvaluareSisteme es in lista2)
            {
                ListViewItem item = new ListViewItem(es.DataEvaluari.ToString());
                item.SubItems.Add(es.TegumenteMucoase);
                item.SubItems.Add(es.SistemCelularSubcutanat);
                item.SubItems.Add(es.SistemGanglionar);
                item.SubItems.Add(es.SistemOsteoarticular);
                item.SubItems.Add(es.SistemCardiovascular);
                item.SubItems.Add(es.SistemRespirator);
                item.SubItems.Add(es.SistemDigestiv);
                item.SubItems.Add(es.SistemGenitoUrinar);
                item.SubItems.Add(es.Snc);
                item.SubItems.Add(es.ExaminareParacilinica);
                item.SubItems.Add(es.Recomandari);
                item.SubItems.Add(es.Concluzii);
                lvEvaluareSisteme.Items.Add(item);
            }

            foreach (ColumnHeader ch in this.lvEvaluareSisteme.Columns)
            {
                ch.Width = -2;
            }

            //evaluare sociala
            lvEvaluareSociala.View = View.Details;
            lvEvaluareSociala.FullRowSelect = true;
            lvEvaluareSociala.GridLines = false;
            namesForm = xmlParser.ExtractAttribute(lvEvaluareSociala.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvEvaluareSociala.Columns.Add(s);

            IList<EvaluareSociala> lista3 = pm.RetrieveEquals<EvaluareSociala>("IdPacient", CNP);
            lvEvaluareSociala.Items.Clear();
            foreach (EvaluareSociala es in lista3)
            {
                ListViewItem item = new ListViewItem(es.DataEvaluari.ToString());
                item.SubItems.Add(es.RezultateSociale);
                item.SubItems.Add(es.RezultateAbilitati);
                item.SubItems.Add(es.ConcluziiAsistent);
                lvEvaluareSociala.Items.Add(item);
            }

            foreach (ColumnHeader ch in this.lvEvaluareSociala.Columns)
            {
                ch.Width = -2;
            }

            //examenoncologic
            lvExamenOncologic.View = View.Details;
            lvExamenOncologic.FullRowSelect = true;
            lvExamenOncologic.GridLines = false;

            namesForm = xmlParser.ExtractAttribute(lvExamenOncologic.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvExamenOncologic.Columns.Add(s);

            IList<ExamenOncologic> lista4 = pm.RetrieveEquals<ExamenOncologic>("IdPacient", CNP);
            lvExamenOncologic.Items.Clear();
            foreach (ExamenOncologic eo in lista4)
            {
                ListViewItem item = new ListViewItem(eo.CavitateBucala);
                item.SubItems.Add(eo.Tegumente);
                item.SubItems.Add(eo.GrupeGangiPalpabile);
                item.SubItems.Add(eo.San);
                item.SubItems.Add(eo.OrgGenFem);
                item.SubItems.Add(eo.CitSecrVaginale);
                item.SubItems.Add(eo.ProstataRect);
                item.SubItems.Add(eo.Altele);
                lvExamenOncologic.Items.Add(item);
            }

            foreach (ColumnHeader ch in this.lvExamenOncologic.Columns)
            {
                ch.Width = -2;
            }


            //fisaExternare
            lvFisaExternare.View = View.Details;
            lvFisaExternare.FullRowSelect = true;
            lvFisaExternare.GridLines = false;

            namesForm = xmlParser.ExtractAttribute(lvFisaExternare.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvFisaExternare.Columns.Add(s);


            IList<FisaExternare> lista5 = pm.RetrieveEquals<FisaExternare>("IdPacient", CNP);
            lvFisaExternare.Items.Clear();
            foreach (FisaExternare fe in lista5)
            {
                ListViewItem item = new ListViewItem(fe.Data.ToString());
                item.SubItems.Add(fe.Vindecat.ToString());
                item.SubItems.Add(fe.Ameliorat.ToString());
                item.SubItems.Add(fe.Transferat.ToString());
                item.SubItems.Add(fe.Agravat.ToString());
                item.SubItems.Add(fe.Decedat.ToString());
                item.SubItems.Add(fe.MotivDeces);
                item.SubItems.Add(fe.AlteStari);
                item.SubItems.Add(fe.DiagnosticExternare);
                item.SubItems.Add(fe.RecomandariEpicriza);
                item.SubItems.Add(fe.PerioadaInternarii);
                item.SubItems.Add(fe.Concediu);
                item.SubItems.Add(fe.Control);
                item.SubItems.Add(fe.Tratament);
                item.SubItems.Add(fe.Semnatura);
                lvFisaExternare.Items.Add(item);
            }
            foreach (ColumnHeader ch in this.lvFisaExternare.Columns)
            {
                ch.Width = -2;
            }


            //fisainternare
            lvFisaInternare.View = View.Details;
            lvFisaInternare.FullRowSelect = true;
            lvFisaInternare.GridLines = false;

            namesForm = xmlParser.ExtractAttribute(lvFisaInternare.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvFisaInternare.Columns.Add(s);

            IList<FiseInternare> lista6 = pm.RetrieveEquals<FiseInternare>("IdPacient", CNP);
            lvFisaInternare.Items.Clear();
            foreach (FiseInternare fi in lista6)
            {
                ListViewItem item = new ListViewItem(fi.DataInternarii.ToString());
                item.SubItems.Add(fi.DiagnosticInternare);
                item.SubItems.Add(fi.Diagnostic72h);
                item.SubItems.Add(fi.DiagnosticTrimitere);
                item.SubItems.Add(fi.MotiveleInternarii);
                item.SubItems.Add(fi.DiagnosticPrincipal);
                item.SubItems.Add(fi.BoliConcomitente);
                item.SubItems.Add(fi.InternatUrgenta);
                item.SubItems.Add(fi.Sectia);
                item.SubItems.Add(fi.Salon);
                lvFisaInternare.Items.Add(item);
            }
            foreach (ColumnHeader ch in this.lvFisaInternare.Columns)
            {
                ch.Width = -2;
            }


            //functiiVitale
            lvFunctiiVitale.View = View.Details;
            lvFunctiiVitale.FullRowSelect = true;
            lvFunctiiVitale.GridLines = false;
            namesForm = xmlParser.ExtractAttribute(lvFunctiiVitale.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvFunctiiVitale.Columns.Add(s);

            IList<FunctiiVitale> lista7 = pm.RetrieveEquals<FunctiiVitale>("IdPacient", CNP);
            lvFunctiiVitale.Items.Clear();
            foreach (FunctiiVitale fv in lista7)
            {
                ListViewItem item = new ListViewItem(fv.DataEvaluarii.ToString());
                item.SubItems.Add(fv.Greutate.ToString());
                item.SubItems.Add(fv.TensiuneArteriala.ToString());
                item.SubItems.Add(fv.Puls.ToString());
                item.SubItems.Add(fv.FrecventaRespiratorie.ToString());
                item.SubItems.Add(fv.Apetit);
                item.SubItems.Add(fv.Diureza);
                item.SubItems.Add(fv.ConcluziiAsistent);
                lvFunctiiVitale.Items.Add(item);
            }
            foreach (ColumnHeader ch in this.lvFunctiiVitale.Columns)
            {
                ch.Width = -2;
            }


            //GraficADS
            lvGraficADS.View = View.Details;
            lvGraficADS.FullRowSelect = true;
            lvGraficADS.GridLines = false;
            namesForm = xmlParser.ExtractAttribute(lvGraficADS.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvGraficADS.Columns.Add(s);

            IList<GraficADS> lista8 = pm.RetrieveEquals<GraficADS>("IdPacient", CNP);
            lvGraficADS.Items.Clear();
            foreach (GraficADS gads in lista8)
            {
                ListViewItem item = new ListViewItem(gads.Data.ToString());
                item.SubItems.Add(gads.MicDejun);
                item.SubItems.Add(gads.Pranz);
                item.SubItems.Add(gads.Cina);
                item.SubItems.Add(gads.Diureza);
                item.SubItems.Add(gads.Scaun);
                item.SubItems.Add(gads.SemnaturaInfirmiera);
                lvGraficADS.Items.Add(item);
            }
            foreach (ColumnHeader ch in this.lvGraficADS.Columns)
            {
                ch.Width = -2;
            }


            //Igiena
            lvIgiena.View = View.Details;
            lvIgiena.FullRowSelect = true;
            lvIgiena.GridLines = false;
            namesForm = xmlParser.ExtractAttribute(lvIgiena.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvIgiena.Columns.Add(s);

            IList<Igiena> lista9 = pm.RetrieveEquals<Igiena>("IdPacient", CNP);
            lvIgiena.Items.Clear();
            foreach (Igiena ig in lista9)
            {
                ListViewItem item = new ListViewItem(ig.Data.ToString());
                item.SubItems.Add(ig.SpalatMainiFata.ToString());
                item.SubItems.Add(ig.Piepatanat.ToString());
                item.SubItems.Add(ig.Barbierit.ToString());
                item.SubItems.Add(ig.ServitMasa.ToString());
                lvIgiena.Items.Add(item);
            }
            foreach (ColumnHeader ch in this.lvIgiena.Columns)
            {
                ch.Width = -2;
            }

            //Imbracat
            lvImbracat.View = View.Details;
            lvImbracat.FullRowSelect = true;
            lvImbracat.GridLines = false;
            namesForm = xmlParser.ExtractAttribute(lvImbracat.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvImbracat.Columns.Add(s);

            IList<Imbracat> lista10 = pm.RetrieveEquals<Imbracat>("IdPacient", CNP);
            lvImbracat.Items.Clear();
            foreach (Imbracat im in lista10)
            {
                ListViewItem item = new ListViewItem(im.Data.ToString());
                item.SubItems.Add(im.ImbracatSingur.ToString());
                item.SubItems.Add(im.HainePuneScoate.ToString());
                item.SubItems.Add(im.CamasaPuneScoate.ToString());
                item.SubItems.Add(im.LegatSireturi.ToString());
                item.SubItems.Add(im.IncheieDescheieNasturi.ToString());
                lvImbracat.Items.Add(item);
            }
            foreach (ColumnHeader ch in this.lvImbracat.Columns)
            {
                ch.Width = -2;
            }


            //Interventie Chirurgicala
            lvInterventieChirurgicala.View = View.Details;
            lvInterventieChirurgicala.FullRowSelect = true;
            lvInterventieChirurgicala.GridLines = false;

            namesForm = xmlParser.ExtractAttribute(lvInterventieChirurgicala.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvInterventieChirurgicala.Columns.Add(s);

            IList<InterventieChirurgicala> lista11 = pm.RetrieveEquals<InterventieChirurgicala>("IdPacient", CNP);
            lvInterventieChirurgicala.Items.Clear();
            foreach (InterventieChirurgicala ic in lista11)
            {
                ListViewItem item = new ListViewItem(ic.Data.ToString());
                item.SubItems.Add(ic.Tehnica);
                item.SubItems.Add(ic.Boala);
                item.SubItems.Add(ic.Semnatura);
                lvInterventieChirurgicala.Items.Add(item);
            }
            foreach (ColumnHeader ch in this.lvInterventieChirurgicala.Columns)
            {
                ch.Width = -2;
            }

            //Invesigatii

            lvInvestigatii.View = View.Details;
            lvInvestigatii.FullRowSelect = true;
            lvInvestigatii.GridLines = false;

            namesForm = xmlParser.ExtractAttribute(lvInvestigatii.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvInvestigatii.Columns.Add(s);

            IList<Investigatii> lista12 = pm.RetrieveEquals<Investigatii>("IdPacient", CNP);
            lvInvestigatii.Items.Clear();
            foreach (Investigatii inv in lista12)
            {
                ListViewItem item = new ListViewItem(inv.Data.ToString());
                item.SubItems.Add(inv.InvestigatiLaborator);
                item.SubItems.Add(inv.ExaminariRadiologice);
                item.SubItems.Add(inv.ExamemeSpecialitate);
                item.SubItems.Add(inv.DiagAnatoPatologice);
                item.SubItems.Add(inv.IstoricBoala);
                item.SubItems.Add(inv.ExamenClinicMedical);
                lvInvestigatii.Items.Add(item);
            }
            foreach (ColumnHeader ch in this.lvInvestigatii.Columns)
            {
                ch.Width = -2;
            }

            //Medicatii
            lvMedicatii.View = View.Details;
            lvMedicatii.FullRowSelect = true;
            lvMedicatii.GridLines = false;
            namesForm = xmlParser.ExtractAttribute(lvMedicatii.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvMedicatii.Columns.Add(s);

            IList<Medicatii> lista13 = pm.RetrieveEquals<Medicatii>("IdPacient", CNP);
            lvMedicatii.Items.Clear();
            foreach (Medicatii med in lista13)
            {
                ListViewItem item = new ListViewItem(med.DataUrgentei.ToString());
                item.SubItems.Add(med.Urgenta);
                item.SubItems.Add(med.DataContributii.ToString());
                item.SubItems.Add(med.Contributii);
                item.SubItems.Add(med.DataDonatii.ToString());
                item.SubItems.Add(med.Donatii);
                item.SubItems.Add(med.DataMedicatie.ToString());
                item.SubItems.Add(med.MedicatieCompensate);
                item.SubItems.Add(med.DataMaterialeSanitare.ToString());
                item.SubItems.Add(med.MaterialeSanitare);
                lvMedicatii.Items.Add(item);
            }
            foreach (ColumnHeader ch in this.lvMedicatii.Columns)
            {
                ch.Width = -2;
            }

            //Persoana Trimitere
            lvPersoanaTrimitere.View = View.Details;
            lvPersoanaTrimitere.FullRowSelect = true;
            lvPersoanaTrimitere.GridLines = false;
            namesForm = xmlParser.ExtractAttribute(lvPersoanaTrimitere.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvPersoanaTrimitere.Columns.Add(s);


            IList<PersoanaTrimitere> lista14 = pm.RetrieveEquals<PersoanaTrimitere>("IdPacient", CNP);
            lvPersoanaTrimitere.Items.Clear();
            foreach (PersoanaTrimitere pt in lista14)
            {
                ListViewItem item = new ListViewItem(pt.Nume);
                item.SubItems.Add(pt.Telefon);
                lvPersoanaTrimitere.Items.Add(item);
            }
            foreach (ColumnHeader ch in this.lvPersoanaTrimitere.Columns)
            {
                ch.Width = -2;
            }


            //Persoana Contact
            lvPersoanaContact.View = View.Details;
            lvPersoanaContact.FullRowSelect = true;
            lvPersoanaContact.GridLines = false;
            namesForm = xmlParser.ExtractAttribute(lvPersoanaContact.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvPersoanaContact.Columns.Add(s);

            IList<PersoaneContact> lista15 = pm.RetrieveEquals<PersoaneContact>("IdPacient", CNP);
            lvPersoanaContact.Items.Clear();
            foreach (PersoaneContact pc in lista15)
            {
                ListViewItem item = new ListViewItem(pc.Nume);
                item.SubItems.Add(pc.Domiciliu);
                item.SubItems.Add(pc.Telefon);
                lvPersoanaContact.Items.Add(item);
            }
            foreach (ColumnHeader ch in this.lvPersoanaContact.Columns)
            {
                ch.Width = -2;
            }

            //Utilitati
            lvUtilitati.View = View.Details;
            lvUtilitati.FullRowSelect = true;
            lvUtilitati.GridLines = false;
            namesForm = xmlParser.ExtractAttribute(lvUtilitati.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvUtilitati.Columns.Add(s);

            IList<Utilitati> lista16 = pm.RetrieveEquals<Utilitati>("IdPacient", CNP);
            lvUtilitati.Items.Clear();
            foreach (Utilitati ut in lista16)
            {
                ListViewItem item = new ListViewItem(ut.Data.ToString());
                item.SubItems.Add(ut.InchisDeschisSertare.ToString());
                item.SubItems.Add(ut.ManevrareTvRadio.ToString());
                item.SubItems.Add(ut.InchisDeschisUsaFereastra.ToString());
                lvUtilitati.Items.Add(item);
            }
            foreach (ColumnHeader ch in this.lvUtilitati.Columns)
            {
                ch.Width = -2;
            }

            // Imagini Medicale
            lvImaginiMedicale.View = View.Details;
            lvImaginiMedicale.FullRowSelect = true;
            lvImaginiMedicale.GridLines = false;
            namesForm = xmlParser.ExtractAttribute(lvImaginiMedicale.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvImaginiMedicale.Columns.Add(s);

            pm = PersistenceManager.Instance;
            IList<ImaginiMedicale> listimagini = pm.RetrieveEquals<ImaginiMedicale>("IdPacient", CNP);

            foreach (ImaginiMedicale imed in listimagini)
            {
                ListViewItem item = new ListViewItem(imed.Id.ToString());
                item.SubItems.Add(imed.Tip);
                item.SubItems.Add(imed.DescriereImagine);
                item.SubItems.Add(imed.Data.ToString());
                item.SubItems.Add(imed.Locatie);

                lvImaginiMedicale.Items.Add(item);
            }
            foreach (ColumnHeader ch in this.lvImaginiMedicale.Columns)
            {
                ch.Width = -2;
            }


            lvDateGrafic.View = View.Details;
            lvDateGrafic.FullRowSelect = true;
            lvDateGrafic.GridLines = false;

            namesForm = xmlParser.ExtractAttribute(lvDateGrafic.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
                lvDateGrafic.Columns.Add(s);

            pm = PersistenceManager.Instance;
            IList<InvestigatiiSenzor> listInvSenzori = pm.RetrieveEquals<InvestigatiiSenzor>("IdPacient", CNP);
            lvDateGrafic.Items.Clear();


            IList<Senzori> listSenzori = pm.RetrieveAll<Senzori>(SessionAction.BeginAndEnd);


            senzorNume.Clear();
            foreach (Senzori s in listSenzori)
            {                
                senzorNume.Add(s.IdSenzor, s.NumeSenzor);
            }

            foreach (InvestigatiiSenzor invsenzor in listInvSenzori)
            {
                ListViewItem item = new ListViewItem(invsenzor.DataInceput.ToString());
                item.SubItems.Add(invsenzor.DataSfarsit.ToString());
                item.SubItems.Add(senzorNume[invsenzor.IdSenzor]);
                lvDateGrafic.Items.Add(item);
            }

            foreach (ColumnHeader ch in this.lvDateGrafic.Columns)
            {
                ch.Width = -2;
            }

 
        }

        private void ListaPacienti_DoubleClick(object sender, EventArgs e)
        {
            for (int i = 0; i < lvImaginiMedicale.Items.Count; i++)
            {
                if (lvImaginiMedicale.Items[i].Selected == true)
                {

                    ListViewItem itm = lvImaginiMedicale.SelectedItems[0];

                    // download image
                    DescarcaImaginiMedicaleForm dim = new DescarcaImaginiMedicaleForm(itm.SubItems[4].Text,
                                                                                      FTPip,
                                                                                      FTPport,
                                                                                      FTPuser,
                                                                                      FTPpass);
                    dim.ShowDialog();

                    // view image
                    if (dim.success == true)
                    {
                        SmartAnnotationForm smartAnotation = new SmartAnnotationForm(itm.SubItems[0].Text, itm.SubItems[4].Text, limba,Program.name);
                        smartAnotation.Show();
                    }

                    dim.Dispose();
                    dim = null;

                    // delete image
                    //System.IO.File.Delete(itm.SubItems[4].Text);
                }

            }
        }

        private void VizualizareGrafic_DoubleClick(object sender, EventArgs e)
        {
            for (int i = 0; i < lvDateGrafic.Items.Count; i++)
            {
                if (lvDateGrafic.Items[i].Selected == true)
                {

                    ListViewItem itm = lvDateGrafic.SelectedItems[0];

                    int id = 0;
                    foreach (int cheie in senzorNume.Keys)
                    {
                        if (senzorNume[cheie].Equals(itm.SubItems[2].Text))
                        {
                            id = cheie;
                            break;
                        }
                    }

                    if (senzorNume[id].Contains("Temperatura"))
                    {
                        GraficTemperatura grafic = new GraficTemperatura(DateTime.Parse(itm.SubItems[0].Text), DateTime.Parse(itm.SubItems[1].Text), id, CNP, limba);
                        grafic.Show();
                    }
                    if (senzorNume[id].Contains("Lumina"))
                    {
                        GraficLumina grafic = new GraficLumina(DateTime.Parse(itm.SubItems[0].Text), DateTime.Parse(itm.SubItems[1].Text), id, CNP, limba);
                        grafic.Show();
                    }
                    if (senzorNume[id].Contains("ECG"))
                    {
                        GraficECG grafic = new GraficECG(DateTime.Parse(itm.SubItems[0].Text), DateTime.Parse(itm.SubItems[1].Text), id, CNP, limba);
                        grafic.Show();
                    }
                    if (senzorNume[id].Contains("BP"))
                    {
                        GraficBP grafic = new GraficBP(DateTime.Parse(itm.SubItems[0].Text), DateTime.Parse(itm.SubItems[1].Text), id, CNP, limba);
                        grafic.Show();
                    }
                }

            }
        }

        public void updateConf()
        {
            this.Text = confName;
            for (int i = 0; i < listUsers.Count; i++)
            {
                if (i == (listUsers.Count - 1))
                {
                     this.gbConectat.Text += listUsers[i];
                }
                else
                {
                    this.gbConectat.Text += listUsers[i] + ", ";
                }                
            }            
        }

        public void upDateVar()
        {
          
            if (listUsers.Count == 1)
            {
                this.gbConectat.Text += (string)listUsers[0];
                this.Text = (string)listUsers[0];
            }
            else if (listUsers.Count > 1)
            {
                this.gbConectat.Text += confName;
                this.Text = confName;
            }
            else
            {
                this.Text = " "+listUsers.Count;
            }
        }

        public void addUser(String usr)
        {
            listUsers.Add(usr);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.text2.Text.Trim().Contains("changeMyName"))
            {
                sendMsgTo(this.text2.Text.Trim());
                this.text2.ResetText();
            }
            else                
                {
                    string g = this.text2.Text.Trim();
                    String tex = " " + myName + " : " + this.text2.Text.Trim();
                    this.text2.ResetText();
                    if (g.Trim().CompareTo("") != 0)
                    {
                        this.text1.Text += " ";
                        this.text1.Text += myName;
                        this.text1.Text += " : ";
                        this.text1.Text += g;
                        this.text1.Text += "\r\n";
                        this.text1.SelectionStart = this.text1.TextLength;
                        this.text1.ScrollToCaret();
                        sendMsgTo(g);
                    }
                }
        }

        private void btCNP_Click(object sender, EventArgs e)
        {
            VizualizarePacientForm vizpacientForm = new VizualizarePacientForm(instance, limba);
            vizpacientForm.Show();
        }

        private void text2_TextChanged(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (this.text2.Text.Trim().Contains("changeMyName"))
                {
                    sendMsgTo(this.text2.Text.Trim());
                    this.text2.ResetText();
                }
                else                   
                    {
                        string g = this.text2.Text.Trim();
                        String tex = " " + myName.ToString() + " : " + this.text2.Text.Trim();
                        this.text2.ResetText();
                        if (g.Trim().CompareTo("") != 0)
                        {
                            this.text1.Text += " ";
                            this.text1.Text += myName;
                            this.text1.Text += " : ";
                            this.text1.Text += g;
                            this.text1.Text += "\r\n";
                            this.text1.SelectionStart = this.text1.TextLength;
                            this.text1.ScrollToCaret();
                            sendMsgTo(g);
                        }
                    }
            }
        }
        public string getMyName(){
            return myName;
        }
       public void sendMsgTo(string msg1)
        {
            string usr;
            //   /conf nume /to us1 /users us1 us2 us3 /msg asd sad sad /from as
            if (isConf)
            {
                for (int i = 0; i < listUsers.Count; i++)
                {
                    string msg = "";
                    msg += "/conf ";
                    msg += this.confName + " ";
                    msg += "/to ";
                    msg += listUsers[i] + " ";
                       
                        string usrs = "";
                        for (int j = 0; j < listUsers.Count; j++)
                        {
                            if (listUsers[j].Equals(listUsers[i]))
                            {
                            //    usrs += myName + " ";
                            }
                            else
                            {
                                usrs += listUsers[j] + " ";
                            }
                        }
                        usrs += this.myName; 
                        msg += "/msg ";
                        msg += msg1;
                        msg += " /users ";                        
                        msg += usrs;
                        msg += " /from " + myName;
                        c.cl.putMsg(msg);
                    }
                
            }
            else
            {
                usr = (string)listUsers[0];
                string msg = "";
                msg += "/msg ";
                msg += usr;
                msg += " ";
                msg += msg1;
                c.cl.putMsg(msg);
            }
        }


       public void sendCNPTo(string msg1)
       {
           string usr;
           //   /conf nume /to us1 /users us1 us2 us3 /msg asd sad sad /from as
           if (isConf)
           {
               for (int i = 0; i < listUsers.Count; i++)
               {
                   string msg = "";
                   msg += "/conf ";
                   msg += this.confName + " ";
                   msg += "/to ";
                   msg += listUsers[i] + " ";

                   string usrs = "";
                   for (int j = 0; j < listUsers.Count; j++)
                   {
                       if (listUsers[j].Equals(listUsers[i]))
                       {
                           //    usrs += myName + " ";
                       }
                       else
                       {
                           usrs += listUsers[j] + " ";
                       }
                   }
                   usrs += this.myName;
                   msg += "/cnp ";
                   msg += msg1;
                   msg += " /users ";
                   msg += usrs;
                   msg += " /from " + myName;
                   c.cl.putMsg(msg);
               }

           }
           else
           {
               usr = (string)listUsers[0];
               string msg = "";
               msg += "/cnp ";
               msg += usr;
               msg += " ";
               msg += msg1;
               c.cl.putMsg(msg);
           }
       }


        private void iesireC_Click(object sender, EventArgs e)
        {
            if (needRemove)
            {
                c.tablist.Remove(this);
                c.tabControl2.Controls.Remove(this);
            }
            else
            {
                c.tabControl2.Controls.Remove(this);
            }
        }

        //columns
        void lvDateGenerale_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvDateGenerale.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvDateGenerale.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvDateGenerale)
            {
                lvDateGenerale.Sorting = SortOrder.None;
            }
            if (lvDateGenerale.Sorting == SortOrder.Ascending)
            {
                lvDateGenerale.Sorting = SortOrder.Descending;
            }
            else
            {
                lvDateGenerale.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvDateGenerale, ref _previouslySortedColumnlvDateGenerale, e.Column);
            lvComparer.SortColumn = e.Column;
            lvDateGenerale.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvDateGenerale.Columns)
            {
                ch.Width = -2;
            }
        }

        void lvEvaluareSisteme_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvEvaluareSisteme.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvEvaluareSisteme.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvDateGenerale)
            {
                lvEvaluareSisteme.Sorting = SortOrder.None;
            }
            if (lvEvaluareSisteme.Sorting == SortOrder.Ascending)
            {
                lvEvaluareSisteme.Sorting = SortOrder.Descending;
            }
            else
            {
                lvEvaluareSisteme.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvDateGenerale, ref _previouslySortedColumnlvEvaluareSisteme, e.Column);
            lvComparer.SortColumn = e.Column;
            lvEvaluareSisteme.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvEvaluareSisteme.Columns)
            {
                ch.Width = -2;
            }
        }

        void lvEvaluareSociala_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvEvaluareSociala.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvEvaluareSociala.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvEvaluareSociala)
            {
                lvEvaluareSociala.Sorting = SortOrder.None;
            }
            if (lvEvaluareSociala.Sorting == SortOrder.Ascending)
            {
                lvEvaluareSociala.Sorting = SortOrder.Descending;
            }
            else
            {
                lvEvaluareSociala.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvEvaluareSociala, ref _previouslySortedColumnlvEvaluareSociala, e.Column);
            lvComparer.SortColumn = e.Column;
            lvEvaluareSociala.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvEvaluareSociala.Columns)
            {
                ch.Width = -2;
            }
        }

        void lvExamenOncologic_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvExamenOncologic.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvExamenOncologic.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvExamenOncologic)
            {
                lvExamenOncologic.Sorting = SortOrder.None;
            }
            if (lvExamenOncologic.Sorting == SortOrder.Ascending)
            {
                lvExamenOncologic.Sorting = SortOrder.Descending;
            }
            else
            {
                lvExamenOncologic.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvExamenOncologic, ref _previouslySortedColumnlvExamenOncologic, e.Column);
            lvComparer.SortColumn = e.Column;
            lvExamenOncologic.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvExamenOncologic.Columns)
            {
                ch.Width = -2;
            }
        }

        void lvFisaExternare_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvFisaExternare.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvFisaExternare.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvFisaExternare)
            {
                lvFisaExternare.Sorting = SortOrder.None;
            }
            if (lvFisaExternare.Sorting == SortOrder.Ascending)
            {
                lvFisaExternare.Sorting = SortOrder.Descending;
            }
            else
            {
                lvFisaExternare.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvFisaExternare, ref _previouslySortedColumnlvFisaExternare, e.Column);
            lvComparer.SortColumn = e.Column;
            lvFisaExternare.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvFisaExternare.Columns)
            {
                ch.Width = -2;
            }
        }

        void lvFisaInternare_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvFisaInternare.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvFisaInternare.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvFisaInternare)
            {
                lvFisaInternare.Sorting = SortOrder.None;
            }
            if (lvFisaInternare.Sorting == SortOrder.Ascending)
            {
                lvFisaInternare.Sorting = SortOrder.Descending;
            }
            else
            {
                lvFisaInternare.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvFisaExternare, ref _previouslySortedColumnlvFisaInternare, e.Column);
            lvComparer.SortColumn = e.Column;
            lvFisaInternare.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvFisaInternare.Columns)
            {
                ch.Width = -2;
            }
        }

        void lvFunctiiVitale_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvFunctiiVitale.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvFunctiiVitale.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvFunctiiVitale)
            {
                lvFunctiiVitale.Sorting = SortOrder.None;
            }
            if (lvFunctiiVitale.Sorting == SortOrder.Ascending)
            {
                lvFunctiiVitale.Sorting = SortOrder.Descending;
            }
            else
            {
                lvFunctiiVitale.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvFisaExternare, ref _previouslySortedColumnlvFunctiiVitale, e.Column);
            lvComparer.SortColumn = e.Column;
            lvFunctiiVitale.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvFunctiiVitale.Columns)
            {
                ch.Width = -2;
            }
        }

        void lvGraficADS_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvGraficADS.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvGraficADS.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvGraficADS)
            {
                lvGraficADS.Sorting = SortOrder.None;
            }
            if (lvGraficADS.Sorting == SortOrder.Ascending)
            {
                lvGraficADS.Sorting = SortOrder.Descending;
            }
            else
            {
                lvGraficADS.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvFisaExternare, ref _previouslySortedColumnlvGraficADS, e.Column);
            lvComparer.SortColumn = e.Column;
            lvGraficADS.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvGraficADS.Columns)
            {
                ch.Width = -2;
            }
        }

        void lvIgiena_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvIgiena.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvIgiena.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvIgiena)
            {
                lvIgiena.Sorting = SortOrder.None;
            }
            if (lvIgiena.Sorting == SortOrder.Ascending)
            {
                lvIgiena.Sorting = SortOrder.Descending;
            }
            else
            {
                lvIgiena.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvFisaExternare, ref _previouslySortedColumnlvIgiena, e.Column);
            lvComparer.SortColumn = e.Column;
            lvIgiena.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvIgiena.Columns)
            {
                ch.Width = -2;
            }
        }

        void lvImbracat_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvImbracat.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvImbracat.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvImbracat)
            {
                lvImbracat.Sorting = SortOrder.None;
            }
            if (lvImbracat.Sorting == SortOrder.Ascending)
            {
                lvImbracat.Sorting = SortOrder.Descending;
            }
            else
            {
                lvImbracat.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvImbracat, ref _previouslySortedColumnlvImbracat, e.Column);
            lvComparer.SortColumn = e.Column;
            lvIgiena.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvImbracat.Columns)
            {
                ch.Width = -2;
            }
        }

        void lvInterventieChirurgicala_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvInterventieChirurgicala.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvInterventieChirurgicala.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvInterventieChirurgicala)
            {
                lvInterventieChirurgicala.Sorting = SortOrder.None;
            }
            if (lvInterventieChirurgicala.Sorting == SortOrder.Ascending)
            {
                lvInterventieChirurgicala.Sorting = SortOrder.Descending;
            }
            else
            {
                lvInterventieChirurgicala.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvImbracat, ref _previouslySortedColumnlvInterventieChirurgicala, e.Column);
            lvComparer.SortColumn = e.Column;
            lvInterventieChirurgicala.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvInterventieChirurgicala.Columns)
            {
                ch.Width = -2;
            }
        }

        void lvInvestigatii_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvInvestigatii.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvInvestigatii.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvInvestigatii)
            {
                lvInvestigatii.Sorting = SortOrder.None;
            }
            if (lvInvestigatii.Sorting == SortOrder.Ascending)
            {
                lvInvestigatii.Sorting = SortOrder.Descending;
            }
            else
            {
                lvInvestigatii.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvImbracat, ref _previouslySortedColumnlvInvestigatii, e.Column);
            lvComparer.SortColumn = e.Column;
            lvInvestigatii.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvInvestigatii.Columns)
            {
                ch.Width = -2;
            }
        }

        void lvMedicatii_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvMedicatii.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvMedicatii.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvMedicatii)
            {
                lvMedicatii.Sorting = SortOrder.None;
            }
            if (lvMedicatii.Sorting == SortOrder.Ascending)
            {
                lvMedicatii.Sorting = SortOrder.Descending;
            }
            else
            {
                lvMedicatii.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvImbracat, ref _previouslySortedColumnlvMedicatii, e.Column);
            lvComparer.SortColumn = e.Column;
            lvMedicatii.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvMedicatii.Columns)
            {
                ch.Width = -2;
            }
        }

        void lvPersoanaTrimitere_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvPersoanaTrimitere.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvPersoanaTrimitere.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvPersoanaTrimitere)
            {
                lvPersoanaTrimitere.Sorting = SortOrder.None;
            }
            if (lvPersoanaTrimitere.Sorting == SortOrder.Ascending)
            {
                lvPersoanaTrimitere.Sorting = SortOrder.Descending;
            }
            else
            {
                lvPersoanaTrimitere.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvImbracat, ref _previouslySortedColumnlvPersoanaTrimitere, e.Column);
            lvComparer.SortColumn = e.Column;
            lvPersoanaTrimitere.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvPersoanaTrimitere.Columns)
            {
                ch.Width = -2;
            }
        }

        void lvPersoanaContact_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvPersoanaContact.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvPersoanaContact.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvPersoanaContact)
            {
                lvPersoanaContact.Sorting = SortOrder.None;
            }
            if (lvPersoanaContact.Sorting == SortOrder.Ascending)
            {
                lvPersoanaContact.Sorting = SortOrder.Descending;
            }
            else
            {
                lvPersoanaContact.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvImbracat, ref _previouslySortedColumnlvPersoanaContact, e.Column);
            lvComparer.SortColumn = e.Column;
            lvPersoanaContact.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvPersoanaContact.Columns)
            {
                ch.Width = -2;
            }
        }

        void lvUtilitati_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewComparer lvComparer;
            if (null == lvUtilitati.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)lvUtilitati.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumnlvUtilitati)
            {
                lvUtilitati.Sorting = SortOrder.None;
            }
            if (lvUtilitati.Sorting == SortOrder.Ascending)
            {
                lvUtilitati.Sorting = SortOrder.Descending;
            }
            else
            {
                lvUtilitati.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(lvImbracat, ref _previouslySortedColumnlvUtilitati, e.Column);
            lvComparer.SortColumn = e.Column;
            lvUtilitati.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.lvUtilitati.Columns)
            {
                ch.Width = -2;
            }
        }
    }
}
